package com.gome.gop.controller.login;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gome.gop.common.Const;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gome.gop.data.entity.Menu;
import com.gome.gop.data.entity.TreeItem;

/**
 * @ClassName: UserController
 * @Description: 登录用户相关信息
 * @author: Administrator
 */
@Controller
public class MainController {
    /**
     * Spring MessageSource实例
     */
    @Resource
    private MessageSource messageSource;
    
    /**
     * 显示主画面
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/mainSubPage.json", method = {RequestMethod.POST, RequestMethod.GET})
    public String index(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "Login_SessionId", required = false) String sessionId,
        @RequestParam(value = "isHistory", required = false) String isHistory, Model model)
        throws IOException, ServletException {
        return "indexPage";
    }
    
    /**
     * Get menu tree
     * 
     * @param sessionId
     * 
     * @return
     */
    @GetMapping(value = "/getMenus")
    @ResponseBody
    public List<TreeItem> getMenuTree(@RequestParam(value = "Login_SessionId", required = false) String sessionId) {
        
        // 获取用户有操作权限的菜单树
        List<Menu> menus = new ArrayList<>();
        Menu menu = new Menu();
        menu.setMenuName("cultivator test1");
        menu.setMenuUrl("www.baidu.com");
        menu.setParentId("sdfsadf");
        menu.setType("2");
        menu.setId("ddd");
        menus.add(menu);
        Menu menu1 = new Menu();
        menu1.setMenuName("cultivator test2");
        menu1.setMenuUrl("sourcePage/test");
        menu1.setParentId("sdfsadf");
        menu1.setType("3");
        menu1.setId("ddd");
        menus.add(menu1);
        List<TreeItem> menuTree = this.toMenuTree(menus, "sdfsadf");
        return menuTree;
    }
    
    /**
     * 转换员工有权限的菜单信息为树状结构.
     *
     * @param menus 菜单列表
     * @param parentId 父菜单ID
     * @return 树状菜单结构
     */
    protected List<TreeItem> toMenuTree(List<Menu> menus, String parentId) {
        List<TreeItem> items = new ArrayList<TreeItem>();
        for (int i = 0; i < menus.size(); i++) {
            Menu menu = menus.get(i);
            
            if (menu.getParentId().equals(parentId)) {
                TreeItem item = new TreeItem();
                if (menu.getType().equals("2")) {
                    item.setId(null);
                    item.setIconCls("app-icon-menu-l1");
                }
                else if (menu.getType().equals("3")) {
                    item.setId(menu.getMenuUrl());
                    item.setIconCls("app-icon-menu-l2");
                }
                item.setText(menu.getMenuName());
                item.addAttribute("id", menu.getId().toString());
                item.setChildren(this.toMenuTree(menus, menu.getId()));
                items.add(item);
            }
        }
        return items;
    }
    
}